package rma.flp;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class RenderPictureController {

	PictureRepository pictureRepository;
	
	@Autowired
	public RenderPictureController(PictureRepository pictureRepository) {
		this.pictureRepository = pictureRepository;
	}
	
	@RequestMapping("/render")
	public void render(@RequestParam("id") Long id, HttpServletResponse response) throws Exception {
		Picture picture = pictureRepository.load(id);
		response.setDateHeader("Last-Modified", picture.getImportedOn().getTime());
		byte[] bytes = picture.getBytes();
		response.setContentLength(bytes.length);
		response.getOutputStream().write(bytes);
	}

}
